package com.kahriboo.log4mon.client;

import com.kahriboo.log4mon.shared.LogEventShared;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;

public class LogEventData {

	public static class LogDS extends DataSource {

		public LogDS(final String id) {

			setID(id);

			setDataFormat(DSDataFormat.CUSTOM);
			final DataSourceDateTimeField timeStampField = new DataSourceDateTimeField("timeStamp", "TimeStamp");
			final DataSourceTextField levelField = new DataSourceTextField("level", "Level");
			final DataSourceTextField threadNameField = new DataSourceTextField("threadName", "Thread");
			final DataSourceTextField classNameField = new DataSourceTextField("className", "Class");
			final DataSourceTextField methodNameField = new DataSourceTextField("methodName", "Method");
			final DataSourceTextField fileNameField = new DataSourceTextField("fileName", "File");
			final DataSourceIntegerField lineNumberField = new DataSourceIntegerField("lineNumber", "Line");
			final DataSourceTextField loggerNameField = new DataSourceTextField("loggerName", "Logger");
			final DataSourceTextField messageField = new DataSourceTextField("message", "Message");
			final DataSourceTextField throwableClassField = new DataSourceTextField("throwableClass", "Throwable Class");
			final DataSourceTextField throwableField = new DataSourceTextField("throwable", "Throwable");

			setFields(timeStampField, levelField, threadNameField, classNameField, methodNameField, fileNameField, lineNumberField, loggerNameField, messageField, throwableClassField, throwableField);

			setClientOnly(true);
		}
	}

	public static DataSource getDataSourceSchema() {
		return new LogDS("id");
	}

	public static LogEventRecord[] convert(final LogEventShared[] source) {
		final LogEventRecord[] result = new LogEventRecord[source.length];
		for (int i = 0; i < result.length; i++) {
			result[i] = new LogEventRecord(source[i]);
		}
		return result;
	}

}
